import utils from '@/mixins/utils.js';
export default {
	name: 'editParkinglotFeature',
	mixins: [utils],
	data() {
		return {
			operatorParkConfigInfo: null,
			inReasonDelList: [],
			outReasonDelList: [],
			isShowMore: false,
			delParkCarNoForceCovertConfigVoList: [],
			forceCovertSearchVal: '',
			filterForceList: []
		};
	},
	created() {
		this.getOperatorParkConfigInfo();
	},
	methods: {
		getOperatorParkConfigInfo() {
			API.fetchGet('/operatorPark/getOperatorParkConfigInfo', {
				parkID: this.$route.query.parkId
			}).then(
				res => {
					if (res.data.parkCarNoForceCovertConfigVoList && res.data.parkCarNoForceCovertConfigVoList.length) {
						res.data.parkCarNoForceCovertConfigVoList.forEach(item => {
							item.isShow = true;
							item.isActive = false;
							item.covertSrcCarNo = JSON.parse(item.covertSrcCarNo).map(item => {
								return {
									carLicensePlate: item
								};
							});
						});
					}

					this.operatorParkConfigInfo = res.data;

					if (
						this.operatorParkConfigInfo.limitVo.openFullLimit ||
						this.operatorParkConfigInfo.limitVo.openNormalLimit ||
						this.operatorParkConfigInfo.limitVo.openVipLimit
					) {
						this.operatorParkConfigInfo.parkCloudDetailVo.openFullLimit = true;
					}
				},
				err => {}
			);
		},

		validateCovertDestCarNo(rule, value, callback) {
			if (value === '') {
				callback(new Error('请输入正确车牌'));
			} else {
				let allCarLicensePlate = [];
				this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.forEach(item => {
					allCarLicensePlate.push(item.covertDestCarNo);
				});
				if (allCarLicensePlate.indexOf(value) !== allCarLicensePlate.lastIndexOf(value)) {
					callback(new Error('正确车牌重复'));
				}
				callback();
			}
		},

		validateCovertSrcCarNo(rule, value, callback) {
			if (value === '') {
				callback(new Error('请输入相近车牌'));
			} else {
				let allCarLicensePlate = [];
				this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.forEach(item => {
					item.covertSrcCarNo.forEach(item => {
						allCarLicensePlate.push(item.carLicensePlate);
					});
				});
				if (allCarLicensePlate.indexOf(value) !== allCarLicensePlate.lastIndexOf(value)) {
					callback(new Error('相近车牌重复'));
				}
				callback();
			}
		},

		delInReason(item, list, index) {
			item.isDelete = 1;
			this.inReasonDelList.push(item);
			list.splice(index, 1);
		},
		delOutReason(item, list, index) {
			item.isDelete = 1;
			this.outReasonDelList.push(item);
			list.splice(index, 1);
		},
		addInReason(list) {
			list.push({
				entranceType: 1,
				id: null,
				isDelete: 0,
				parkId: this.$route.query.parkId,
				reasonContent: ''
			});
		},
		addOutReason(list) {
			list.push({
				entranceType: 2,
				id: null,
				isDelete: 0,
				parkId: this.$route.query.parkId,
				reasonContent: ''
			});
		},
		saveEdit() {
			let saveData = JSON.parse(JSON.stringify(this.operatorParkConfigInfo));
			saveData.checkInReason = saveData.checkInReason.concat(this.inReasonDelList);
			saveData.checkOutReason = saveData.checkOutReason.concat(this.outReasonDelList);
			saveData.parkCarNoForceCovertConfigVoList = saveData.parkCarNoForceCovertConfigVoList.concat(
				this.delParkCarNoForceCovertConfigVoList
			);
			saveData.parkCarNoForceCovertConfigVoList.forEach(item => {
				delete item['isActive'];
				item.covertSrcCarNo = JSON.stringify(item.covertSrcCarNo.map(item => item.carLicensePlate));
			});
			saveData.parkId = this.$route.query.parkId;
			API.fetchPost('/operatorPark/updateOperatorParkConfigInfo', saveData).then(
				res => {
					if (res.status === 1) {
						this.showToast('修改成功', 1, 1500);
						setTimeout(() => {
							this.back();
						}, 1500);
					} else {
						this.showToast(res.message, 2, 1500);
					}
				},
				err => {
					console.log(err);
				}
			);
		},
		testForm() {
			this.$refs.operatorParkConfigInfo.validate(valid => {
				if (valid) {
					console.log('valid');
					this.saveEdit();
				} else {
					console.log('error!!');
					return false;
				}
			});
		},
		back() {
			this.$router.push({
				name: 'allParkinglotInfo',
				query: this.$route.query
			});
		},
		limitChange(val) {
			if (!val) {
				this.operatorParkConfigInfo.limitVo.openNormalLimit = false;
				this.operatorParkConfigInfo.limitVo.openVipLimit = false;
				this.operatorParkConfigInfo.limitVo.openFullLimit = false;
			}
		},
		openFullLimitChange(val) {
			if (val) {
				this.operatorParkConfigInfo.limitVo.openNormalLimit = false;
				this.operatorParkConfigInfo.limitVo.openVipLimit = false;
				this.resetForm('operatorParkConfigInfo');
			}
		},
		otherLimiteChange(val) {
			if (val) {
				this.operatorParkConfigInfo.limitVo.openFullLimit = false;
				this.resetForm('operatorParkConfigInfo');
			}
		},
		resetForm(formName) {
			this.$refs[formName].resetFields();
		},
		addCarNoForceCovert() {
			this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.unshift({
				covertDestCarNo: '',
				covertSrcCarNo: [
					{
						carLicensePlate: ''
					}
				],
				id: null,
				isDelete: 0,
				isShow: true
			});
		},
		addCovertSrcCarNo(list) {
			list.push({
				carLicensePlate: ''
			});
		},
		removeCovertSrcCarNo(list, index) {
			list.splice(index, 1);
		},
		removeparkCarNoForceCovertConfigVoItem(list, index, curItem) {
			if (curItem.id) {
				let copy_item = JSON.parse(JSON.stringify(curItem));
				copy_item.isDelete = 1;
				this.delParkCarNoForceCovertConfigVoList.push(copy_item);
			}
			list.splice(index, 1);
		},
		carNoForceCovertFlagChange(val) {
			if (val && !this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.length) {
				this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.unshift({
					covertDestCarNo: '',
					covertSrcCarNo: [
						{
							carLicensePlate: ''
						}
					],
					id: null,
					isDelete: 0
				});
			}
		}
	},
	watch: {
		forceCovertSearchVal(val) {
			if (val) {
				this.isShowMore = true;
				this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.forEach((item, index) => {
					item.isShow = false;
					item.isActive = false;

					item.covertSrcCarNo.forEach(item => {
						item.isActive = false;
					});
				});
				this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.forEach((item, index) => {
					if (
						item.covertDestCarNo.includes(val) ||
						item.covertSrcCarNo.filter(item => item.carLicensePlate.includes(val)).length
					) {
						item.isShow = true;
						if (item.covertDestCarNo.includes(val)) {
							item.isActive = true;
						} else {
							item.isActive = false;
						}

						if (item.covertSrcCarNo.filter(item => item.carLicensePlate.includes(val)).length) {
							item.covertSrcCarNo.forEach(item => {
								if (item.carLicensePlate.includes(val)) {
									item.isActive = true;
								} else {
									item.isActive = false;
								}
							});
						}
						return item;
					}
				});
			} else {
				this.isShowMore = false;
				this.operatorParkConfigInfo.parkCarNoForceCovertConfigVoList.forEach(item => {
					item.isShow = true;
				});
			}
		}
	}
};
